package com.sczmgk.hotline.utils;

import android.content.Context;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessageBody;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.utils.MyLog;
import com.hyphenate.exceptions.HyphenateException;
import com.sczmgk.hotline.MyApp;
import com.sczmgk.hotline.db.impl.ChatMsgDaoImpl;
import com.sczmgk.hotline.entity.ChatMsgBean;

import static com.hyphenate.chat.EMGCMListenerService.TAG;
import static com.sczmgk.hotline.MyApp.loginUserUid;

/**
 * Created by Administrator on 2017/5/19.
 */

public class HxUtils {


    /**
     * 存入消息
     *
     * @param myMsg
     * @param context
     */
    public static void insertOrUpdateDB(ChatMsgBean myMsg, Context context) {

        com.hyphenate.easeui.utils.MyLog.d(TAG, "<<<<<<<<<<<<<<<<<<<添加或更新数据库<<<<<<<<<<<<<<<<<<<<<<");

        ChatMsgDaoImpl chatMsgDao = ChatMsgDaoImpl.getIntance(context);
        //1.判断消息是否存在
        //2.存在则修改
        //3.不存在则添加
        boolean exists = chatMsgDao.isExists(myMsg.getChatid());
        if (!exists) {
            com.hyphenate.easeui.utils.MyLog.d(TAG, "消息不存在,添加---> chatid： " + myMsg.getChatid() + "    消息内容：" + myMsg.getTextmsg());
            //不存在,添加
            chatMsgDao.insert(myMsg);
        } else {
            MyLog.d(TAG, "消息存在,更新      ---> chatid： " + myMsg.getChatid() + "    消息内容：" + myMsg.getTextmsg());
            //存在,更新
            ChatMsgBean oldMsg = chatMsgDao.findByQuery(myMsg.getChatid());
            myMsg.setId(oldMsg.getId());
            chatMsgDao.updata(myMsg);
        }

    }
    public static void insertOrUpdateDB(EMMessage message,Context context){
        ChatMsgBean myMsg = createMyMsg(message, true);
        insertOrUpdateDB(myMsg,context);
    }


    /**
     * 将环信消息生成我们的消息
     *
     * @param message 环信消息
     * @return
     */
    public static ChatMsgBean createMyMsg(EMMessage message, boolean waitUpload) {

        int type = 0;
        String typename = "";
        EMMessage.Type mType = message.getType();
        switch (mType) {
            case TXT:
                type = 1;
                typename = mType.name();
                break;
        }
        //msgBody
        EMMessageBody msgbody = message.getBody();
        String body = msgbody.toString();
        MyLog.d(TAG, "body: " + body);
        String subStr = body.substring(5, body.length() - 1);
        //msgtime
        long msgtime = message.getMsgTime();
        //msgId
        String msgid = message.getMsgId();
        //username
        String username = message.getUserName();
        //isAsked
        int isasked = (message.isAcked() ? 1 : 0);
        //isdelivered
        int isdelivered = (message.isDelivered() ? 1 : 0);
        //islistened
        int islistened = (message.isListened() ? 1 : 0);
        // TODO   此处设置用户id
        EMMessage.Direct direct = message.direct();
        EMMessage.Status status = message.status();
        int mStatus = 0;
        switch (status) {
            case INPROGRESS:
                mStatus = ChatMsgBean.MSG_STATUS_SENDING;
                break;
            case SUCCESS:
                mStatus = ChatMsgBean.MSG_STATUS_SEND_SUCCESS;
                break;
            case FAIL:
                mStatus = ChatMsgBean.MSG_STATUS_SEND_FAIL;
                break;
        }

        String chatId = "";

        int isOnline = ChatMsgBean.ATTR_VALUE_ONLINE;
        /**
         * 是否需要上传到服务器
         */
        int needUpload = ChatMsgBean.DO_NOT_NPLOAD;
        try {
            chatId = message.getStringAttribute(ChatMsgBean.ATTR_KEY_CHATID);

        } catch (HyphenateException e) {
            e.printStackTrace();
            chatId = EaseChatFragment.createMyMsgId();
        }

        try {//设置消息接收来源
            if (message.getIntAttribute(ChatMsgBean.ATTR_KEY_RECEIVE)==ChatMsgBean.ATTR_VALUE_ANDROID) {
                isOnline = ChatMsgBean.ATTR_VALUE_ANDROID;
            } else {
                isOnline = ChatMsgBean.ATTR_VALUE_ONLINE;
            }
        } catch (HyphenateException e) {
            e.printStackTrace();
        }

        if (message.direct() == EMMessage.Direct.SEND) {
            try {//设置消息发送目标
                if (message.getIntAttribute(ChatMsgBean.ATTR_KEY_SEND_TO)==ChatMsgBean.ATTR_VALUE_ANDROID) {
                    isOnline = ChatMsgBean.ATTR_VALUE_ANDROID;
                } else {
                    isOnline = ChatMsgBean.ATTR_VALUE_ONLINE;
                }
            } catch (HyphenateException e) {
                e.printStackTrace();
            }
        }

        //标记是否为待上传

        if (waitUpload) {
            needUpload = ChatMsgBean.NEED_UPLOAD;
        } else {
            needUpload = ChatMsgBean.DO_NOT_NPLOAD;
        }
        ChatMsgBean msgBean = ChatMsgBean.getTextMsg(needUpload, isOnline, loginUserUid, MyApp.imei, isOnline == ChatMsgBean.ATTR_VALUE_ONLINE? "online":"xiaoi", subStr, mStatus, type, msgtime, msgid, (direct == EMMessage.Direct.SEND ? 0 : 1), username, isasked, isdelivered, islistened, chatId);
        return msgBean;
    }


}
